#include "plic.h"
#include "im110gw_it.h"

#ifndef PLIC_VEC
extern int puts(const char *s);
static volatile PLIC_IRQHandlePtr PLIC_IRQHandler[PLIC_NUM_INTERRUPTS];

void PLIC_IRQDefaultHandler (void) 
{
    puts("you need overwrite this irq isr\n");
}

void  __attribute__ ((interrupt)) external_handler(void)
{
    uint32_t id = PLIC_GetClaimIRQ();
    PLIC_IRQHandler[id]();
PLIC_CompleteIRQ(id);
}

void PLIC_Init(PLIC_Priority threshold)
{
    for (uint32_t i = 0; i < PLIC_NUM_INTERRUPTS; i++)
    {
        PLIC_IRQHandler[i] = PLIC_IRQDefaultHandler;
    }

    PLIC_SetThreshold(threshold);
}

void PLIC_IRQInit(PLIC_IRQ irq, PLIC_Priority priority, PLIC_IRQHandlePtr handler)
{
    PLIC_SetPriority(irq, priority);
    PLIC_EnableIRQ(irq);
    PLIC_IRQHandler[irq] = handler;
}

#endif
